#include "../include/client.h"

void send_request(const char *action, const char *username, const char *password) {
    int sock = 0;
    struct sockaddr_in serv_addr;
    char buffer[BUFFER_SIZE] = {0};
    
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        printf("Socket creation error\n");
        return;
    }
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(SERVER_PORT);
    
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
        printf("Invalid address / Address not supported\n");
        return;
    }
    
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("Connection failed\n");
        return;
    }
    
    cJSON *json = cJSON_CreateObject();
    cJSON_AddStringToObject(json, "action", action);
    cJSON_AddStringToObject(json, "username", username);
    cJSON_AddStringToObject(json, "password", password);
   // {"action":"requtest", "username", "testuser", "password": "password123"}
    
    char *json_string = cJSON_Print(json);
    printf("send-- str: %s\n", json_string);
    send(sock, json_string, strlen(json_string), 0);
    cJSON_Delete(json);
    free(json_string);
    
    read(sock, buffer, BUFFER_SIZE);
    //Registration successful
    printf("Server response: %s\n", buffer);
    close(sock);
}

int main() {
    send_request("register", "testuser", "password123");
    send_request("login", "testuser12", "password123");
    return 0;
}
